/**
 * Copyright 2012 Roger Cockshutt
 * krakenbrau@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.parallelrl.util;

import java.util.Arrays;
import java.util.List;
import java.util.Vector;

/**
 * @author Roger Cockshutt
 *
 */
public class ComponentPair extends ImmutablePair<String, String> {

	/**
	 * @param componentName
	 * @param componentValue
	 */
	public ComponentPair(String componentName, String componentValue) {
		super(componentName, componentValue);
	}
	
	public String name() {
		return a;
	}
	
	public String value() {
		return b;
	}
	
	public Integer intValue() {
		return Integer.valueOf(b);
	}
	
	public Double doubleValue() {
		return Double.valueOf(b);
	}
	
	public Long longValue() {
		return Long.valueOf(b);
	}
	
	public Character charValue() {
		return Character.valueOf(b.charAt(0));
	}
	
	public Point2D coordinateValue() {
		return Point2D.valueOf(b);
	}
	
	public List<String> listValue() {
		return Arrays.asList(b.split("\\s*,\\s*"));
	}
	
	public List<Integer> listIntegerValue() {
		Vector<Integer> list = new Vector<>();
		for (String s : b.split("\\s*,\\s*")) {
			list.add(Integer.valueOf(s));
		}
		return list;
	}
	
	@Override
	public String toString() {
		String cmp = a + ": '" + b + "'";
		return cmp;
	}
	
	public static ComponentPair valueOf(String s) {
		String[] split = s.trim().split(":");
		String name = null;
		String value = null;
		if (split.length >= 2) {
			name = split[0].trim();
			split = split[1].trim().split("'");
			if (!split[0].equals("")) {
				value = split[0].trim();
			}
		}
		if (name != null) {
			if (value == null) {
				value = "";
			}
			return new ComponentPair(name, value);
		}
		return null;
	}

}
